<template>
  <el-dialog v-if="dialogFormVisible" v-el-drag-dialog :visible.sync="dialogFormVisible" :close-on-click-modal="false" width="900px" :title="winTitle" @close="handleDialogClose">
    <div v-if="loading" v-loading="loading" class="el-dialog__body" />
    <el-form v-else class="form-container">
      <div class="el-table el-table--fit el-table--border">
        <div class="el-table__body-wrapper">
          <table class="el-table__body" cellspacing="0" cellpadding="0" border="0">
            <tbody>
              <tr>
                <td class="tdlbl tdw17 ">文件标题</td>
                <td class="tdcont tdw83" colspan="3">
                  {{ wfInstance.title }}
                </td>
              </tr>
              <tr>
                <td class="tdlbl tdw17 ">文件类型</td>
                <td class="tdcont tdw33">  {{ wfInstance.wfName }} </td>
                <td class="tdlbl tdw17 ">处理环节</td>
                <td class="tdcont tdw33">  {{ curTN.tnNodeName }}  </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </el-form>
    <SubmitHandle v-if="!loading&&wfJsonData!=undefined&&submitHandle!=null" ref="submitHandle" :cur-t-n="curTN" :wf-instance="wfInstance" :submit-handle="submitHandle" :wf-json-data="wfJsonData" :common-idea-list="commonIdeaList" :submit-template-list="submitTemplateList" />
    <div v-if="!loading&&wfJsonData!=undefined&&submitHandle!=null" slot="footer" class="dialog-footer">
      <el-button plain icon="el-icon-close" type="default" @click="handleDialogClose">取 消</el-button>
      <el-button icon="el-icon-check" :disabled="doing" type="primary" @click="handleSubmitForm">确 定</el-button>
    </div>

    <SelectOuterDeputy />
  </el-dialog>
</template>

<script>
import { getQueryObject } from '@/global/utils/index'

export default {
    name: 'ModifyWflogSubmitFile',
    components: {

    },
    props: {

    },
    data() {
        return {
            winTitle: '',
            dialogFormVisible: false,
            wfParams: {},
            wfInstance: {},
            vo: {},
            wfJsonData: {},
            submitHandle: null,
            commonIdeaList: [],
            submitTemplateList: [],
            curTN: {},
            loginUser: this.$store.getters.loginUser,
            attachments: [],
            doNextAttach: null, // 代办佐证材料
            loading: false,
            doing: false
        }
    },
    computed: {

    },
    watch: {

    },
    created() {
        this.$bus.on('showModifyWflogSubmitFile', this.showModifyWflogSubmitFile)
    },
    beforeDestroy() {
        this.$bus.off('showModifyWflogSubmitFile', this.showModifyWflogSubmitFile)
    },
    mounted() {

    },
    methods: {
        showModifyWflogSubmitFile(wflog, actionType) {
            const _this = this
            if (_this.loading) {
                return
            }
            _this.loading = true
            _this.dialogFormVisible = true

            if (actionType === 'redo') {
                this.winTitle = '重办（' + wflog.staffName + '）'
            } else if (actionType === 'agency') {
                this.winTitle = '代办（' + wflog.staffName + '）'
            }

            this.wfParams = getQueryObject(location.href)
            this.wfParams.wfNodelogId = wflog.id
            this.wfParams.openType = 'FlowAdminSubmit'
            this.wfParams.wfInstanceId = wflog.wfInstanceId

            this.$wfEngine.loadWfInstance(this, this.wfParams).then(res => {
                if (res.code === 'success') {
                    if (actionType !== 'agency') {
                        delete this.wfInstance.passenger.doNextNeedAttach
                    }
                    _this.loading = false
                } else {
                    this.$alert(res.message, '操作提示', { type: 'error', callback: () => {
                        _this.handleDialogClose()
                        _this.loading = false
                    } })
                }
            })
        },
        handleSubmitForm() {
            const _this = this

            const submitType = 'FlowAdminSubmit'
            const res = this.$wfEngine.checkSubmitHandle(this, submitType)
            if (res.code !== 'success') {
                this.$alert(res.message, '操作提示', { type: 'warning' })
                return
            }

            if (this.wfInstance.passenger.doNextNeedAttach === true && this.doNextAttach === null) {
                this.$alert('请上传佐证材料！', '操作提示', { type: 'warning' })
                return
            }

            this.$wfEngine.checkOuterDeputy(this, this.wfParams).then(res => {
                if (res.code !== 'success') {
                    this.$alert(res.message, '操作提示', { type: 'error' })
                } else {
                    _this.doing = true

                    _this.$vnode.context.$vnode.context.$refs.attachments.uploadWflowAttach(this).then(res => {
                        if (res.code === 'success') {
                            this.$wfEngine.saveWfInstance(this, submitType, this.wfParams).then(res => {
                                if (res.code === 'success') {
                                    this.handleDialogClose()

                                    if (this.$vnode.context.modifyType === 'donext') {
                                        window.location.reload()
                                    } else {
                                        this.$vnode.context.loadWfNodelogList()
                                    }
                                } else {
                                    this.$alert(res.message, '操作提示', { type: 'error' })
                                }
                                _this.doing = false
                            })
                        } else {
                            _this.$alert(res.message, '操作提示', { type: 'error' })
                            _this.doing = false
                        }
                    })
                }
            })
        },
        handleDialogClose() {
            this.dialogFormVisible = false
            this.wfInstance = {}
            this.vo = {}
            this.wfJsonData = {}
            this.submitHandle = null
            this.commonIdeaList = []
            this.submitTemplateList = []
            this.curTN = {}
            this.attachments = []
        }
    }
}
</script>

<style scoped lang="scss">

</style>
